package com.trace.demo.repository;

import com.trace.demo.entity.TraceCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.Optional;

@Repository
public interface TraceCodeRepository extends JpaRepository<TraceCode, Long> {

    Optional<TraceCode> findByCode(String code);
    Optional<TraceCode> findByCodeOrTraceCode(String code, String traceCode);
    Long countByCreatedAtAfter(Date dateTime);
    @Modifying
    @Query("UPDATE TraceCode t SET t.frequency = COALESCE(t.frequency, 1) + 1 WHERE t.code = :code")
    @Transactional
    void updateFrequencyByCode(String code);
}
